import 'dart:convert';

import 'package:car_baidu/core/constant/initialize_data.dart';
import 'package:car_baidu/core/model/login_info_model.dart';
import 'package:car_baidu/core/service/http_config.dart';
import 'package:car_baidu/ui/widget/web_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class LLMineListConfig {
  static final List<Map<String, dynamic>> MineListData = [
    {"iconName": "asset/image/mine_list_fwxy.png", "title": "服务协议"},
    {"iconName": "asset/image/mine_list_dcgl.png", "title": "多车管理"},
    {"iconName": "asset/image/mine_list_wdbyysd.png", "title": "我的保养预约单"},
    {"iconName": "asset/image/mine_list_wdbd.png", "title": "我的保单"},
    {"iconName": "asset/image/mine_list_wddd.png", "title": "我的订单"},
    {"iconName": "asset/image/mine_list_esc.png", "title": "我的二手车"},
    {"iconName": "asset/image/mine_list_gcyyd.png", "title": "购车预约单"},
  ];

  static final List<Widget> MineListItems =
      List.generate(MineListData.length, (index) {
    Map<String, dynamic> map = MineListData[index];
    return Builder(builder: (BuildContext context) {
      LLLoginInfoModel? loginModel = LLInitializeData.loginModel;
      return Container(
        color: Colors.white,
        child: ListTile(
          leading: Image.asset(map["iconName"]),
          title: Text(map["title"]),
          trailing: Image.asset("asset/image/mine_list_next.png"),
          onTap: () {
            if (index == 0) {
              jumpTpServicePrivacy(
                  context, map["title"], "asset/html/service_content.html");
            }

            if (index == 3) {
              if (loginModel != null) {
                Navigator.of(context).pushNamed(LLWebViewScreen.routeName,
                    arguments: {
                      "title": map["title"],
                      "url": "${HttpConfig.baseUrl}${loginModel.strInsurance}"
                    });
              }
            }
          },
        ),
      );
    });
  });

  static void jumpTpServicePrivacy(
    BuildContext context,
    String title,
    String path,
  ) async {
    String data = await rootBundle.loadString(path);
    final String htmlUrl = Uri.dataFromString(data,
            mimeType: 'text/html',
            encoding: Encoding.getByName('utf-8'),
            base64: true)
        .toString();
    Navigator.of(context).pushNamed(LLWebViewScreen.routeName,
        arguments: {"title": title, "url": htmlUrl});
  }
}
